package com.hanyao.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanyao.reggie.bean.Category;
import com.hanyao.reggie.bean.Dish;
import com.hanyao.reggie.bean.Setmeal;
import com.hanyao.reggie.common.CustomExcept;
import com.hanyao.reggie.mapper.CategoryMapper;
import com.hanyao.reggie.service.CategoryService;
import com.hanyao.reggie.service.DishService;
import com.hanyao.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 寒钥
 * @version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> disQueryWrapper = Wrappers.lambdaQuery();
        disQueryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(disQueryWrapper);
        if(count>0){
            throw new CustomExcept("当前分类下还有菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = Wrappers.lambdaQuery();
        setmealQueryWrapper.eq(Setmeal::getCategoryId,id);
        count = setmealService.count(setmealQueryWrapper);
        if(count>0){
            throw new CustomExcept("当前分类下还有套餐，不能删除");
        }
        categoryService.removeById(id);
    }
}
